package com.gykfbxxh.imovie.manager;


import com.gykfbxxh.imovie.bean.config.ChannelItem;
import com.gykfbxxh.imovie.config.TypeConstant;

import java.util.ArrayList;
import java.util.List;

/**
 * 管理新闻栏目的管理类
 * Created by xuxinghai on 2016/7/14.
 */
public class AppChannelManager {

    private static AppChannelManager mInstance;

    //栏目表名
//    private String channelTable = NewsDBUtil.getInstance().getNewsChannelTable();

    /**
     * 默认的用户选择频道列表
     * */
    private static List<ChannelItem> defaultUserChannels;

    static {
        defaultUserChannels = new ArrayList<ChannelItem>();
        defaultUserChannels.add(new ChannelItem("", "全部", 1, 1, TypeConstant.CHANNEL_TYPE_NEWS));
        defaultUserChannels.add(new ChannelItem("0010000000000000000", "动作", 2, 1,TypeConstant.CHANNEL_TYPE_NEWS));
        defaultUserChannels.add(new ChannelItem("1000000000000000000", "喜剧", 3, 1,TypeConstant.CHANNEL_TYPE_NEWS));
        defaultUserChannels.add(new ChannelItem("0000010000000000000", "剧情", 4, 1,TypeConstant.CHANNEL_TYPE_NEWS));
        defaultUserChannels.add(new ChannelItem("0000001000000000000", "犯罪", 5, 1,TypeConstant.CHANNEL_TYPE_VIDEO));
        defaultUserChannels.add(new ChannelItem("0000000000000000010", "惊悚", 6, 1,TypeConstant.CHANNEL_TYPE_VIDEO));
        defaultUserChannels.add(new ChannelItem("0100000000000000000", "爱情", 7, 1,TypeConstant.CHANNEL_TYPE_NEWS));
        defaultUserChannels.add(new ChannelItem("0000000000000001000", "家庭", 8, 1,TypeConstant.CHANNEL_TYPE_NEWS));


//        defaultUserChannels.add(new ChannelItem("0001000000000000000", "恐怖", 5, 1,TypeConstant.CHANNEL_TYPE_VIDEO));
//        defaultUserChannels.add(new ChannelItem("0000100000000000000", "科幻", 6, 1,TypeConstant.CHANNEL_TYPE_NEWS));
//        defaultUserChannels.add(new ChannelItem("0000000100000000000", "奇幻", 9, 1,TypeConstant.CHANNEL_TYPE_NEWS));
//        defaultUserChannels.add(new ChannelItem("0000000010000000000", "战争", 10, 1,TypeConstant.CHANNEL_TYPE_NEWS));
//        defaultUserChannels.add(new ChannelItem("0000000001000000000", "悬疑", 11, 1,TypeConstant.CHANNEL_TYPE_VIDEO));
//        defaultUserChannels.add(new ChannelItem("0000000000100000000", "动画", 11, 1,TypeConstant.CHANNEL_TYPE_VIDEO));
//        defaultUserChannels.add(new ChannelItem("0000000000010000000", "伦理", 11, 1,TypeConstant.CHANNEL_TYPE_VIDEO));
//        defaultUserChannels.add(new ChannelItem("0000000000001000000", "记录", 11, 1,TypeConstant.CHANNEL_TYPE_VIDEO));
//        defaultUserChannels.add(new ChannelItem("0000000000000100000", "传记", 11, 1,TypeConstant.CHANNEL_TYPE_VIDEO));
//        defaultUserChannels.add(new ChannelItem("0000000000000010000", "歌舞", 11, 1,TypeConstant.CHANNEL_TYPE_VIDEO));
//        defaultUserChannels.add(new ChannelItem("0000000000000000100", "历史", 11, 1,TypeConstant.CHANNEL_TYPE_VIDEO));
//        defaultUserChannels.add(new ChannelItem("0000000000000000001", "文艺", 11, 1,TypeConstant.CHANNEL_TYPE_VIDEO));

    }

    private AppChannelManager(){}

    /**
     * 获取唯一实例
     * @return
     */
    public static AppChannelManager getInstance(){
        if(mInstance == null){
            mInstance = new AppChannelManager();
        }
        return mInstance;
    }


    /**
     * 获取用户的频道
     * @return 数据库存在用户配置  ? 数据库内的用户选择频道 : 默认用户选择频道 ;
     */
    public List<ChannelItem> getUserChannel() {

//        Object cacheList = NewsDBUtil.getInstance().listCache("selected= ?", new String[]{"1"},"orderid", NewsDBUtil.getInstance().getNewsChannelTable(),null);
//        if (cacheList != null && !((List) cacheList).isEmpty()) {
//            List<Map<String, String>> maplist = (List) cacheList;
//            int count = maplist.size();
//            List<ChannelItem> list = new ArrayList<ChannelItem>();
//
//            for (int i = count-1; i >=0 ; i--) {
//                ChannelItem navigate = new ChannelItem();
//                navigate.setId(maplist.get(i).get("id"));
//                navigate.setName(maplist.get(i).get("name"));
//                navigate.setOrderId(Integer.valueOf(maplist.get(i).get("orderid")));
//                navigate.setSelected(Integer.valueOf(maplist.get(i).get("selected")));
//                navigate.setTypeid(maplist.get(i).get("type"));
//                list.add(navigate);
//            }
//            if(!list.isEmpty()){
//                return list;
//            }
//        }
        return defaultUserChannels;
    }

//    /**
//     * 清除所有频道
//     */
//    public void deleteAllChannle(){
//        NewsDBUtil.getInstance().clearTable(channelTable);
//    }
//
//    /**
//     * 保存用户频道到数据库
//     * @param userList
//     */
    public void saveUserChannel(List<ChannelItem> userList) {
        if(userList!=null&&userList.size()>0) {
            defaultUserChannels.clear();
            defaultUserChannels.addAll(userList);
        }
    }
//
    /**
     * 更新数据库的频道数据
     * @param list
     */
    public void updateServicesChannel(List<ChannelItem> list){
        saveUserChannel(list);
    }

}
